package org.example.controller;


import lombok.RequiredArgsConstructor;
import org.example.domain.ResponseResult;
import org.example.domain.dto.MenuLIstDto;
import org.example.domain.entity.Menu;
import org.example.enums.AppHttpCodeEnum;
import org.example.service.MenuService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService menuService;


    @GetMapping("/menuTree")
    public ResponseResult menuTree(){

        return menuService.menuTree();
    }

    @GetMapping("/roleMenus/{id}")
    public ResponseResult roleMenus(@PathVariable Integer id){
        List<Menu> menus = menuService.selectMenuByRoleId(id);
        menus.stream().map(p -> p.getId()).collect(Collectors.toList());
        return ResponseResult.okResult(menus);
    }


    @PostMapping("/getMenuList")
    public ResponseResult getMenuList(@RequestBody MenuLIstDto menuLIstDto){

        return menuService.getMenuLIst(menuLIstDto);
    }

    @GetMapping("/treeList")
    public ResponseResult treeList(){
        return ResponseResult.okResult(menuService.treeList());
    }

    @PostMapping("/addMenu")
    public ResponseResult addMenu(@RequestBody Menu menu){
        return menuService.addMenu(menu);
    }

    @PutMapping("/updateMenu")
    public ResponseResult updateMenu(@RequestBody Menu menu){
        if(menu.getId() == menu.getParentId()){
            return ResponseResult.errorResult(AppHttpCodeEnum.MENU_PARENT_ERROR);
        }
        return menuService.updateMenu(menu);
    }
}
